package com.zpdhy.system.controller;

import com.zpdhy.system.model.SysRole;
import com.zpdhy.system.model.UserInfo;
import com.zpdhy.system.service.SysRoleService;
import com.zpdhy.system.shiro.DynamicUpdatePermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * Created by kedong on 2016/12/30.
 */
@Controller
@RequestMapping("/role/")
public class SysRoleController {

    @Resource
    private DynamicUpdatePermission updatePermission;

    @Autowired
    private SysRoleService sysRoleService;

    @RequestMapping("/list")
    public String list(Map<String, Iterable<SysRole>> map) {
        map.put("list", sysRoleService.findAll());
        return "role/list";
    }

    @RequestMapping("/add")
    public String add(Map<String, Object> map) {
        return "role/add";
    }

    @PostMapping("saveOrUpdate")
    public String saveOrUpdate(SysRole sysRole) {
        sysRoleService.save(sysRole);
        List<UserInfo> userInfoList = sysRole.getUserInfos();
        if (userInfoList != null) {
            for (UserInfo userInfo : userInfoList) {
                updatePermission.updateUserPermission(userInfo.getUserName());
            }
        }
        updatePermission.updateUserPermission("kedong");
        return "redirect:list";
    }
}
